import pymongo
import time

client = pymongo.MongoClient()
bboxdb = client.bbox
cts = lambda :int(time.time())

# log 
class Log(object):
    logs = bboxdb.logs

    @classmethod
    def add_log(cls, ts, email, space_id, tags, content):
        log = { "email": email,
                "space_id": space_id,
                "tags": tags,
                "ts": ts,
                "content": content}
        
        cls.logs.insert(log)

    @classmethod
    def get_by_email(cls, email, space_id):
        result = cls.logs.find({"email":email, "space_id":space_id}).sort("ts", pymongo.DESCENDING )
        return result

# user
class User(object):
    users = bboxdb.users
    #__tablename__ = 'user'
    #id = Column(Integer, primary_key=True)
    #email = Column(String(100), index=True)
    #password = Column(String(100))
    #nickname = Column(String(100))
    #ctime = Column(DATETIME, index=True)

    @classmethod
    def valid(cls, nickname, passwd):
        user = cls.users.find_one({"nickname":nickname})
        if user and user["password"] == passwd:
            return True
        return False

    @classmethod
    def register(cls, email, password, nickname):
        # unique email
        if cls.users.find_one({"email":email}):
            return (False, 'email exists')
        if cls.users.find_one({"nickname": nickname}):
            return (False, 'nickname exists')
        u = {"email":email, "password":password, "nickname":nickname,
            "ctime": int(time.time()) }
        cls.users.insert(u)
        return (True, '')

    @classmethod
    def get_by_nickname(cls, nickname):
        return cls.users.find_one({"nickname":nickname})

    @classmethod
    def get_by_email(cls, email):
        return cls.users.find_one({"email":email})

class UserDetail(object):
    #__tablename__ = 'user_detail'

    #id = Column(Integer, primary_key=True)
    #intro = Column(String(1024))
    #gender = Column(String(5))
    #birthday = Column(String(12))
    #hometown = Column(String(255))
    #city = Column(String(255))
    #job = Column(String(255))
    #pic_url = Column(String(255))
    userdetails = bboxdb.userdetails

    @classmethod
    def get_by_email(cls, email):
        return cls.userdetails.find_one({"email":email})

    @classmethod
    def update(cls, data):
        email = data.get("email")
        return cls.userdetails.update({"email":email}, data, upsert=True)

class UserSpace(object):
    spaces = bboxdb.spaces

    @classmethod
    def get_by_id(cls, _id):
        return cls.spaces.find_one({'_id': _id})

    @classmethod
    def get_by_full(cls, email, space):
        return cls.spaces.find_one({'email':email, 'space': space})

    @classmethod
    def add(cls, email, space):
        cls.spaces.insert({'email':email, 'space':space})

    @classmethod
    def set(cls, email, old_space, new_space):
        data = {'email':email, 'new_space': new_space}
        return cls.spaces.update({'email': email, 'space': old_space}, {"$set": data}, upsert=True)

    @classmethod
    def get_spaces(cls, email):
        l = []
        for i in cls.spaces.find({'email':email}):
            l.append(i['space'])
        return l

def format():
    bboxdb.logs.remove()
    bboxdb.users.remove()
    bboxdb.userdetails.remove()
    bboxdb.spaces.remove()

